package com.yang.live;

import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.EulerAngle;
import org.bukkit.util.Vector;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public final class Live extends JavaPlugin implements Listener, CommandExecutor {

    private Map<UUID, UUID> spectators = new HashMap<>(); // 保存观战者和被观战者的映射

    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);
        getCommand("spectate").setExecutor(this);
    }

    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event) {
        Player player = event.getEntity();
        if (!spectators.containsKey(player.getUniqueId())) {
            spectators.put(player.getUniqueId(), null); // 将玩家加入观战者列表，设置为未观战任何玩家
            player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1)); // 设置为透明状态
        }
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event) {
        Player player = event.getPlayer();
        spectators.remove(player.getUniqueId()); // 玩家退出游戏时从观战者列表中移除
    }

    // 玩家选择观战其他玩家
    public void spectatePlayer(Player spectator, Player target) {
        spectators.put(spectator.getUniqueId(), target.getUniqueId()); // 更新观战者和被观战者的映射
        // 将观战者设置为观察者模式
        spectator.setGameMode(GameMode.SPECTATOR);
        spectator.teleport(target); // 传送观战者到被观战者位置

        // 创建虚拟手
        ArmorStand hand = (ArmorStand) target.getWorld().spawnEntity(target.getLocation(), EntityType.ARMOR_STAND);
        hand.setVisible(false);
        hand.setSmall(true);
        hand.setGravity(false);
        hand.setArms(true);

        new BukkitRunnable() {
            @Override
            public void run() {
                if (!spectator.isOnline() || !target.isOnline()) {
                    hand.remove();
                    cancel();
                    return;
                }

                // 更新虚拟手的位置
                hand.teleport(target.getLocation().add(0, 1.5, 0));

                // 将被观察者手上的物品设置到虚拟手上
                ItemStack handItem = target.getInventory().getItemInMainHand();
                hand.getEquipment().setItemInMainHand(handItem != null ? handItem.clone() : new ItemStack(Material.AIR));
            }
        }.runTaskTimer(this, 0, 1);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (label.equalsIgnoreCase("spectate")) {
            if (sender instanceof Player) {
                Player player = (Player) sender;
                if (args.length == 1) {
                    // 获取目标玩家
                    Player target = Bukkit.getPlayer(args[0]);
                    if (target != null) {
                        // 调用 spectatePlayer 方法
                        spectatePlayer(player, target);
                        player.sendMessage("你正在观战玩家 " + target.getName());
                        return true;
                    } else {
                        player.sendMessage("目标玩家不存在或不在线");
                    }
                } else {
                    player.sendMessage("用法: /spectate <玩家名>");
                }
            } else {
                sender.sendMessage("该命令只能由玩家执行");
            }
        }
        return false;
    }
}
